<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP starting page</title>
<link rel="stylesheet" type="text/css"
	href="js/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="style/mystyle.css" />
<script type="text/javascript" src="js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext/ext-all.js"></script>
<script>
	Ext.onReady(function() {

		var statement = "null";
		var sarea = new Ext.form.TextArea({
			id : 'statementtext',
			xtype : 'textarea',
			height : 240,
			width : 500
		});

		var proxy_1 = new Ext.data.HttpProxy({
			url : 'scriptquery.svt?kind=showtables'
		});

		var reader_1 = new Ext.data.JsonReader({}, [{
			name : 'tablename'
		}, {
			name : 'hivetablename'
		}, {
			name : 'hdfspath'
		}]);

		var store_1 = new Ext.data.Store({
			proxy : proxy_1,
			reader : reader_1
		});

		store_1.load();

		var tables = new Ext.grid.GridPanel({
			store : store_1,
			margins : '5 0 5 5',
			title : 'Hive表',
			frame : true,
			width : 300,
			region : 'center',
			columns : [{
				id : 'tablename',
				header : '数据库表名',
				width : 150,
				sortable : true,
				dataIndex : 'tablename',
				align : 'center'
			}, {
				id : 'hivetablename',
				header : 'Hive表名',
				width : 200,
				sortable : true,
				dataIndex : 'hivetablename',
				align : 'center'
			}, {
				id : 'hdfspath',
				header : 'HDFS 路径',
				width : 200,
				sortable : true,
				dataIndex : 'hdfspath',
				align : 'center'
			}]
		});

		var script = new Ext.Panel({
			region : 'west',
			frame : true,
			margins : '5 0 5 5',
			title : '脚本输入',
			width : 400,
			tbar : [
					new Ext.Button({
						text : '检查',
						handler : function() {
							statement = Ext.getCmp("statementtext").getValue();
							var requestConfig = {
								url : 'scriptquery.svt?kind=compile',
								params : {
									statement : statement
								},
								callback : function(option, success, response) {
									var msg = [success, option.customer,
											response.responseText];
									if (response.responseText == "1") {
										Ext.MessageBox.alert("Prompt",
												"Success.");
									} else {
										Ext.MessageBox
												.alert("Prompt", "Error.");
									}
								}
							}
							Ext.Ajax.request(requestConfig);
						}
					}),
					{
						xtype : 'tbseparator'
					},
					new Ext.Button({
						text : '查询',
						handler : function() {
							statement = Ext.getCmp("statementtext").getValue();
							var requestConfig = {
								url : 'scriptquery.svt?kind=launch',
								params : {
									statement : statement
								},
								callback : function(option, success, response) {
									var msg = [success, option.customer,
											response.responseText];
									if (response.responseText == "1") {
										Ext.MessageBox.alert("Prompt",
												"Start Job.");
									} else {
										Ext.MessageBox
												.alert("Prompt", "Error.");
									}
								}
							}
							Ext.Ajax.request(requestConfig);
						}
					}),
					{
						xtype : 'tbseparator'
					},
					new Ext.Button({
						text : '重置',
						handler : function() {
								Ext.getCmp("statementtext").setValue("");
						}
					})
					/*,
					{
						xtype : 'tbseparator'
					},
					new Ext.Button({
						text : 'Show Hive Tables',
						handler : function() {
							statement = Ext.getCmp("statementtext").getValue();
							var requestConfig = {
								url : 'commonquery.svt?kind=showtable',
								params : {
									statement : statement
								},
								callback : function(option, success, response) {
									var msg = [success, option.customer,
											response.responseText];
									//grid.getStore().reload();
								}
							}
							Ext.Ajax.request(requestConfig);
						}
					})*/],
			items : [sarea]
		});

		var proxy_5 = new Ext.data.HttpProxy({
			url : 'scriptquery.svt?kind=console'
		});

		var reader_5 = new Ext.data.JsonReader({}, [{
			name : 'taskid'
		}, {
			name : 'progress'
		}, {
			name : 'hql'
		}, {
			name : 'submittime'
		}]);

		var store_5 = new Ext.data.Store({
			proxy : proxy_5,
			reader : reader_5
		});

		store_5.load();

		var buttons = new Ext.grid.GridPanel({
			store : store_5,
			region : 'east',
			frame : true,
			margins : '5 0 5 5',
			title : '控制面板',
			width : 350,
			title : '任务监控',
			tbar : [new Ext.Button({
				text : '刷新',
				handler : function() {

					var requestConfig = {
						url : 'scriptquery.svt?kind=refresh',
						params : {},
						callback : function(option, success, response) {
						}
					}
					Ext.Ajax.request(requestConfig);

				}
			})],
			columns : [{
				id : 'submittime',
				header : '任务ID',
				//width : 90,
				sortable : true,
				dataIndex : 'submittime',
				align : 'center'
			},{
				id : 'hql',
				header : 'HQL查询语句',
				//width : 90,
				sortable : true,
				dataIndex : 'hql',
				width : 300,
				align : 'center'
			}, {
				id : 'Progress',
				header : 'progress',
				//width : 90,
				sortable : true,
				dataIndex : 'progress',
				align : 'center'
			}, {
				id : 'taskid',
				header : '其他信息',
				//width : 90,
				sortable : true,
				dataIndex : 'taskid',
				align : 'center'
			}]
		});
		self.setInterval(function() {
			buttons.getStore().reload();
		}, 1000);

		var container_1 = new Ext.Panel({
			layout : 'border',
			region : 'north',
			height : 300,
			margins : '5 5 0 5',
			items : [tables, script, buttons]
		});

		var details = new Ext.Panel({
			region : 'center',
			frame : true,
			margins : '5 5 5 5',
			title : '结果集',
			tbar:[new Ext.Button({
					text:'导出文本结果',
					handler:function(){
						Ext.MessageBox.prompt("Prompt","请输入导出目标路径：",function(button,text){
							if(text == ""){
								Ext.MessageBox.alert("Prompt","目标路径无效");
								return;
							}
							var content = Ext.getDom("innerPanel").innerHTML;
// 							alert(text);
								var requestConfig = {
						url:'commonquery.svt?kind=exportresult',
						params:{resultset:content,dir:text},
						callback:function(option,success,response){
							var msg = [success,option.customer,response.responseText];
							if(response.responseText=="1"){
							Ext.MessageBox.alert("Prompt","成功");
							}else{
							Ext.MessageBox.alert("Prompt","失败");}
						}
					}
					 Ext.Ajax.request(requestConfig);
						});
					}
				})],
			items:[new Ext.form.TextArea({
				id:'innerPanel',
				width:1150,
				height:300,
				bodyStyle :'overflow-x:visible;overflow-y:scroll' 
			})]
		});

	
	buttons.addListener('cellclick', consoleClick);
	
	function consoleClick(grid, rowIndex, columnIndex, e){
					var record = grid.getStore().getAt(rowIndex); //Get the Record   
					var selectRows = grid.getSelectionModel().getSelections();
					var taskId  = selectRows[0].get("taskid");
						var requestConfig = {
						url:'scriptquery.svt?kind=result',
						params:{taskId:taskId},
						callback:function(option,success,response){
							var msg = [success,option.customer,response.responseText];
							Ext.getDom("innerPanel").innerHTML = response.responseText;
						}
					}
					 Ext.Ajax.request(requestConfig);
					
	}

		var viewport = new Ext.Viewport({
			layout : 'border',
			items : [details, container_1]
		});

	});
</script>
</head>

<body>
</body>
</html>
